package com.franklin.ideaplugin.api.ui.editor;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

import javax.swing.*;

/**
 * @author Ye Junhui
 * @since 2023/5/23
 */
public class FormatJsonButton extends JButton {

    private final CustomEditor customEditor;

    public FormatJsonButton(CustomEditor customEditor) {
        super("Format");
        this.customEditor = customEditor;
        this.addActionListener(e -> {
            String jsonStr = this.customEditor.getText();
            try {
                JSONObject jsonObject = JSON.parseObject(jsonStr);
                String prettyJsonStr = jsonObject.toJSONString(JSONWriter.Feature.PrettyFormat);
                this.customEditor.setText(prettyJsonStr);
            } catch (Exception exception) {

            }
        });
    }

}
